﻿using System;
using System.Collections.Generic;
using System.Text;
using AAA.Base;
using System.Web.UI.WebControls;
using System.Data;
using AAA.Base.Util;

namespace AAA.Web
{
    public class CheckBoxListHelper
    {
        public static string[] GetSelectedItems(CheckBoxList clstList)
        {
            return GetSelectedItems(clstList, CheckFieldEnum.Value);
        }

        public static string[] GetSelectedItems(CheckBoxList clstList, CheckFieldEnum eReturnField)
        {
            List<string> lstSelectedItem = new List<string>();

            for (int i = 0; i < clstList.Items.Count; i++)
            {
                if (!clstList.Items[i].Selected)
                    continue;

                lstSelectedItem.Add(eReturnField == CheckFieldEnum.Item
                                        ?   clstList.Items[i].Text
                                        :   clstList.Items[i].Value);
            }

            return (string[])lstSelectedItem.ToArray();
        }

        public static void FillItems(CheckBoxList clstList, string[] strItems)
        {
            FillItems(clstList, strItems, strItems);
        }

        public static void FillItems(CheckBoxList clstList, string[] strItems, string[] strValues)
        {
            FillItems(clstList, strItems, strValues, ArrayHelper.Create<bool>(true, strItems.Length));
        }

        public static void FillItems(CheckBoxList clstList, string[] strItems, string[] strValues, bool[] isChecked)
        {
            ListItem lstItem;

            clstList.Items.Clear();
            for (int i = 0; i < strItems.Length; i++)
            {
                lstItem = new ListItem(strItems[i], strValues[i]);
                lstItem.Selected = isChecked[i];
                clstList.Items.Add(lstItem);
            }
        }

        public static void FillItems(CheckBoxList clstList, DataTable dtSource, string strItemField)
        {
            FillItems(clstList, dtSource, strItemField, strItemField);
        }

        public static void FillItems(CheckBoxList clstList, DataTable dtSource, string strItemField, string strValueField)
        {
            FillItems(clstList, dtSource, strItemField, strValueField, true);
        }

        public static void FillItems(CheckBoxList clstList, DataTable dtSource, string strItemField, string strValueField, bool isChecked)
        {
            clstList.Items.Clear();
            ListItem lstItem;
            for (int i = 0; i < dtSource.Rows.Count; i++)
            {
                lstItem = new ListItem(dtSource.Rows[i][strItemField].ToString(),
                                       dtSource.Rows[i][strValueField].ToString());
                lstItem.Selected = isChecked;
                clstList.Items.Add(lstItem);
            }
        }

        public static void FillItems(CheckBoxList clstList, DataTable dtSource, string strItemField, string strValueField, 
                                     string strCheckField, List<string> lstCheckContent, CheckTypeEnum eContentType)
        {
            clstList.Items.Clear();
            int iConvert = (eContentType == CheckTypeEnum.Checked ? 1 : -1);
            ListItem lstItem;
            
            for (int i = 0; i < dtSource.Rows.Count; i++)
            {                
                lstItem = new ListItem(dtSource.Rows[i][strItemField].ToString(),
                                       dtSource.Rows[i][strValueField].ToString());
                lstItem.Selected = (lstCheckContent.IndexOf(dtSource.Rows[i][strCheckField].ToString()) + 0.1) * iConvert > 0;
                clstList.Items.Add(lstItem);
            }
        }

    }
}
